using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

public class CardCaoTran
{
    // public Transform _trans;
    public GameObject _card;
    public Vector3 _pos;
    public Vector3 _scale;
    public MeshRenderer _meshMat;
}
public class CardCao : MonoBehaviour
{
    private CardCaoTran[] mCardCaoTransArr = new CardCaoTran[7];
    private Vector3[] mCardNormalPosArr = new Vector3[7];
    
    private static CardCao ins;
    public static CardCao Ins
    {
        get { return ins; }
    }

    private void Awake()
    {
        ins = this;
        
        for (int i = 0; i < mCardCaoTransArr.Length; i++)
        {
            Transform trans = transform.Find((i+1).ToString());
            mCardCaoTransArr[i] = new CardCaoTran();
            mCardCaoTransArr[i]._card = trans.gameObject;
            mCardCaoTransArr[i]._pos = trans.position;
            mCardCaoTransArr[i]._scale = trans.localScale;
            mCardCaoTransArr[i]._meshMat = transform.Find((i+1)+"/MaJiang").GetComponent<MeshRenderer>();

            mCardNormalPosArr[i] = trans.position;
        }
    }

    //显示不显示卡槽
    public void OnShowCardCao(bool isshow)
    {
        gameObject.SetActive(isshow);
    }
    /// <summary>
    /// 获取缩放大小
    /// </summary>
    /// <returns></returns>
    public float OnGetCardScale()
    {
        return mCardCaoTransArr[0]._scale.x;
    }

    
    /// <summary>
    /// 获取哪个牌的位置
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public Vector3 OnGetCardPosByIndex(int index)
    {
        return mCardCaoTransArr[index]._pos;
    }
    /// <summary>
    /// 获取卡槽相关信息
    /// </summary>
    /// <returns></returns>
    public CardCaoTran[] OnGetCardCaoArr()
    {
        return mCardCaoTransArr;
    }

    /// <summary>
    /// 设置meshrenderer的mat
    /// </summary>
    /// <param name="index"></param>
    /// <param name="mat"></param>
    public void OnSetCardCaoMeshMat(int index,Material mat)
    {
        mCardCaoTransArr[index]._card.transform.position = mCardNormalPosArr[index];
        mCardCaoTransArr[index]._card.SetActive(true);
        mCardCaoTransArr[index]._meshMat.material = mat;
    }
    /// <summary>
    /// 关闭某一个牌
    /// </summary>
    /// <param name="index"></param>
    public void OnCloseCard(int index)
    {
        mCardCaoTransArr[index]._card.SetActive(false);
    }

    /// <summary>
    /// 消除
    /// </summary>
    /// <param name="middleindex"></param>
    public void OnSetCardCaoXiaochu(int middleindex)
    {
        Vector3 middlepos = mCardCaoTransArr[middleindex]._pos;
        mCardCaoTransArr[middleindex - 1]._card.transform.DOMove(middlepos, 0.1f).SetEase(Ease.Linear).OnComplete(() =>
        {
            mCardCaoTransArr[middleindex - 1]._card.SetActive(false);
        });
        mCardCaoTransArr[middleindex + 1]._card.transform.DOMove(middlepos, 0.1f).SetEase(Ease.Linear).OnComplete(() =>
        {
            mCardCaoTransArr[middleindex + 1]._card.SetActive(false);
            mCardCaoTransArr[middleindex]._card.SetActive(false);
        });
    }

    /// <summary>
    /// 刷新牌面
    /// </summary>
    /// <param name="cardfacearr"></param>
    public void OnUpdateCardFace(int[] cardfacearr)
    {
        for (int i = 0; i < mCardCaoTransArr.Length; i++)
        {
            mCardCaoTransArr[i]._card.SetActive(cardfacearr[i]>0);
            if (cardfacearr[i]>0)
            {
                OnSetCardCaoMeshMat(i, MJCtrl.Instance().OnGetMaterialBymjId(cardfacearr[i]));
            }
        }
    }
}
